package com.coyote.web.mybatisplus_config;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * @author wangqinmin
 * @date 2023/3/25 13:05
 * @description: 共产主义接班人
 * <p>
 * 对mybatis实体类中指定字段使用
 * @TableField(fill = FieldFill.INSERT)
 * @Schema(description = "创建时间", example = "2023-03-23 00:00:00")
 * private Date createTime;
 * <p>
 * @TableField(fill = FieldFill.INSERT_UPDATE)
 * @Schema(description = "修改时间", example = "2023-03-23 12:00:00")
 * private Date updateTime;
 */
@Component
public class MyMetaObjectHandler implements MetaObjectHandler {


    public MyMetaObjectHandler() {
        System.out.println("MyMetaObjectHandler initialized!");
    }


    /**
     * 插入时的填充策略
     *
     * @param metaObject 元对象
     */
    @Override
    public void insertFill(MetaObject metaObject) {
        // 实现插入时的字段自动填充逻辑
        this.strictInsertFill(metaObject, "createTime", Date.class, new Date());
        this.strictInsertFill(metaObject, "updateTime", Date.class, new Date());
    }

    /**
     * 更新时的填充策略
     *
     * @param metaObject 元对象
     */
    @Override
    public void updateFill(MetaObject metaObject) {
        this.strictInsertFill(metaObject, "updateTime", Date.class, new Date());
    }
}
